#include <recv.h>
#include <config.h>
#include <string.h>
#include <pub_lib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdio.h>
/**
 * @brief 获取当前进程的sock
 * 
 * @return int 进程sock 成功;其他 失败
*/
static int GetSocket()
{
    int sock;
    struct sockaddr_un server_addr;

    if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr,"can't get socket!");
        return -1;
    }

    // 设置服务器地址
    memset(&server_addr, 0, sizeof(struct sockaddr_un));
    server_addr.sun_family = AF_UNIX;
    strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);


    // 连接服务器
    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un)) == -1) {
        fprintf(stderr,"can't connect to socket!");
        close(sock);
        return -2;
    }
    return sock;
}

/**
 * @brief 选择并设置字体
 * @param - pcFontsFile	: 字体文件 (XXX.TTF)
 * @param - dwFontSize	: 字体大小
 * @return  0 成功;其他 失败
 */
int PubSetFontsDetail(char *pcFontsFile, uint32_t dwFontSize)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_SET_FONT_DETAIL;
    send_buf[1] = 22;

    memcpy(send_buf+2,&dwFontSize,4);
    memcpy(send_buf+6,pcFontsFile,16);

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }

    if(send_buf[3])
    {
        fprintf(stderr,"Comand not execute");
        return -4;
    }
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 获取当前使用的字体信息
 * 
 * @param - pcFontsFile	: 字体文件 (XXX.TTF)
 * @param - pFontSize	: 字体大小
 * @return int 
 */
int PubGetFontsDetail(char *pcFontsFile, uint32_t *pFontSize)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_GET_FONT_DETAIL;
    send_buf[1] = 4;

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 22) != 22) {
       fprintf(stderr,"No response received");
       return -3;
    }
    memcpy(pFontSize,send_buf+2,4);
    memcpy(pcFontsFile,send_buf+6,16);
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 设置字体色和背景色
 * 
 * @param iState 1：字体色;0：背景色
 * @param Color 颜色（RGB888格式）
 * @return int 0 成功;其他 失败
*/
int PubSetColor(int iState,uint32_t Color)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_SET_COLOR;
    send_buf[1] = 7;
    send_buf[2] = iState;

    memcpy(send_buf+3,&Color,4);

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }

    if(send_buf[3])
    {
        fprintf(stderr,"Comand not execute");
        return -4;
    }
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 获取字体色和背景色
 * 
 * @param iState iState 1：字体色;0：背景色
 * @param Color Color 颜色（RGB888格式）
 * @return int 0 成功;其他 失败
*/
int PubGetColor(int iState,uint32_t *Color)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_GET_COLOR;
    send_buf[1] = 4;
    send_buf[2] = iState;

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 6) != 6) {
       fprintf(stderr,"No response received");
       return -3;
    }
    memcpy(Color,send_buf+2,4);
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 设置Dlog过滤等级
 * 
 * @param level 过滤等级
 * @return int 0 成功;其他 失败
 */
int PubSetDlogLvl(uint8_t level)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_SET_DLOG_LVL;
    send_buf[1] = 3;
    send_buf[2] = level;


    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }

    if(send_buf[3])
    {
        fprintf(stderr,"Comand not execute");
        return -4;
    }
    // 关闭套接字
    close(sock);

    return 0;
}


/**
 * @brief 获取Dlog过滤等级
 * 
 * @param level 过滤等级
 * @return int 0 成功;其他 失败
 */
int PubGetDlogLvl(uint8_t *level)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_GET_DLOG_LVL;
    send_buf[1] = 2;

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }
    
    memcpy(level,send_buf+2,1);
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 设置Dlog输出格式
 * 
 * @param level 过滤等级
 * @param set   格式
 * @return int  0 成功;其他 失败
 */
int PubSetDlogFmt(uint8_t level,uint16_t set)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_SET_DLOG_FMT;
    send_buf[1] = 5;
    send_buf[2] = level;

    memcpy(send_buf+3,&set,2);

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }

    if(send_buf[3])
    {
        fprintf(stderr,"Comand not execute");
        return -4;
    }
    // 关闭套接字
    close(sock);

    return 0;
}

/**
 * @brief 获取Dlog输出格式
 * 
 * @param level 过滤等级
 * @param set   格式
 * @return int  0 成功;其他 失败
 */
int PubGetDlogFmt(uint8_t level,uint16_t *set)
{
    int sock;
    char send_buf[24];

    sock = GetSocket();
    if(sock < 0){
        return -1;
    }

    send_buf[0] = CMD_GET_DLOG_FMT;
    send_buf[1] = 3;
    send_buf[2] = level;

    if (send(sock, send_buf,send_buf[1], 0) == -1) {
        fprintf(stderr,"Send message failed");
        close(sock);
        return -2;
    }

    // 读取服务器发送的数据
    memset(send_buf, 0, sizeof(send_buf));
    if (read(sock, send_buf, 4) != 4) {
       fprintf(stderr,"No response received");
       return -3;
    }
    
    memcpy(set,send_buf+2,2);
    // 关闭套接字
    close(sock);

    return 0;
}